<?php
class CommonModel extends model
{
	static $_instance = null;

	static function getInstance ()
	{
		if (is_null(self::$_instance)) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	// 全部机构
	function getAgencyAllList()
	{
		return $this->sql('select * from crm_agency where stop=0 order by parent ')->s();
	}
	
	// 权限机构
	function getAgencyList($p)
	{
		$p["id"] = ArrayTool::ArrayToIDs(CommonModel::getInstance()->getAgencyAllList(),$p["agency"]);
		return $this->file("AgencyPage",$p)->s();
	}
	
	// 权限机构
	function getAgencyComboxOne($p)
	{
		$p["id"] = ArrayTool::ArrayToIDs(CommonModel::getInstance()->getAgencyAllList(),$p["agency"]);
		return $this->sql("select * from crm_agency where id in ([id]) ",$p)->s();
	}
	
	// 权限机构
	function getAgencyComboxTwo($p)
	{
		$p["id"] = ArrayTool::ArrayToIDs(CommonModel::getInstance()->getAgencyAllList(),$p["agency"]);
		return $this->sql("select * from crm_agency where id in ([id]) and find_in_set([business],business) ",$p)->s();
	}
	
	// 区域
	function getAreaList()
	{
		return $this->sql("select * from crm_area")->s();
	}
	
	// 配件
	function getDeviceCount()
	{
		$brand = trim(P("key",""));
		$types = trim(P("types",""));
		$brand = trim(P("brand",""));
		$pattern = trim(P("pattern",""));
		$sql = "select count(*) as c from crm_device where 1=1 ";
		if($name != ""){$sql .= " and name like '%".$name."%'";}
		if($types != ""){$sql .= " and types ='".$types."'";}
		if($brand != ""){$sql .= " and brand ='".$brand."'";}
		if($pattern != ""){$sql .= " and pattern ='".$pattern."'";}

		$ret = $this->sql($sql)->s(1);
		return $ret["c"];
	}
	
	// 配件
	function getDeviceList()
	{
		$brand = trim(P("key",""));
		$types = trim(P("types",""));
		$brand = trim(P("brand",""));
		$pattern = trim(P("pattern",""));
		$sortField = trim(P("sortField","id"));
		$sortOrder = trim(P("sortOrder","asc"));
		$p = P("pageIndex",0);
		$r = P("pageSize",20);
		$sql = "select * from crm_device where 1=1 ";
		if($name != ""){$sql .= " and name like '%".$name."%'";}
		if($types != ""){$sql .= " and types ='".$types."'";}
		if($brand != ""){$sql .= " and brand ='".$brand."'";}
		if($pattern != ""){$sql .= " and pattern ='".$pattern."'";}
		$sql .= " order by ".$sortField." ".$sortOrder;
		$sql .= " limit [start],[end]";

		return $this->sql($sql,array("start"=>$r*$p,"end"=>$r))->s();
	}
	
	// 机构部门
	function getPositionList($p){
		return $this->sql("select * from crm_position where agency = [agency]",$p)->s();
	}
	
	// 员工
	function getStaffCount($id){
		$sqltemp="select cs.login_id as uid,su.agency_id,ca.name as agency_name,su.user,cp.name as position_name,cs.* from crm_staff as cs,sys_user as su left join crm_position as cp on cp.id = su.position_id left join crm_agency as ca on ca.id = su.agency_id where cs.stop=0 and su.stop=0 and cs.login_id = su.id";
		
		$name = trim(P("key",""));
		
		$sql = "select count(*) as c from ($sqltemp) a where agency_id in ($id) ";
		if($name != ""){$sql .= " and name like '%".$name."%'";};
		
		$ret = $this->sql($sql)->s(1);
		return $ret["c"];
	}
	
	function getStaff($id){
		$sqltemp="select cs.login_id as uid,su.agency_id,ca.name as agency_name,su.user,cp.name as position_name,cs.* from crm_staff as cs,sys_user as su left join crm_position as cp on cp.id = su.position_id left join crm_agency as ca on ca.id = su.agency_id where cs.stop=0 and su.stop=0 and cs.login_id = su.id";
		
		$name = trim(P("key",""));
		$sortField = trim(P("sortField","agency_id"));
		$sortOrder = trim(P("sortOrder","asc"));
		$p = P("pageIndex",0);
		$r = P("pageSize",20);
		//echo $id;exit;
		$sql = "select * from ($sqltemp) a where agency_id in ($id) ";
		if($name != ""){$sql .= " and name like '%".$name."%'";}
		$sql .= " order by ".$sortField." ".$sortOrder;
		$sql .= " limit [start],[end]";
		
		return $this->sql($sql,array("start"=>$r*$p,"end"=>$r))->s();
	}
	
	// 员工信息
	function getStaffInfo($p){
		return $this->sql("select * from (select cs.login_id as uid,su.user,su.agency_id,ca.name as agency_name,cs.* from crm_staff as cs,sys_user as su left join crm_agency as ca on ca.id = su.agency_id where  cs.login_id = su.id) a where uid=[uid]",$p)->s(1);
	}
	
	
	// 顾客
	function getCustomerCount($p){
		
		$sql = "select count(*) as c from ( select cs.login as uid,su.user,ca.name as area,cs.* 
                                            from crm_customer as cs left join crm_area as ca on ca.id = cs.area_id,
                                                 sys_user as su
                                            where cs.login = su.id
		                                   ) a 
		        where 1=1 ";

		if($p["name"] != ""){$sql .= " and name like '%".$p["name"]."%'";}
	    if($p["area"] != ""){$sql .= " and area_id in (".$p["area"].")";}
		
		$ret = $this->sql($sql)->s(1);
		return $ret["c"];
	}
	
	function getCustomer($pa){
		$sortField = trim(P("sortField","id"));
		$sortOrder = trim(P("sortOrder","asc"));
		$p = P("pageIndex",0);
		$r = P("pageSize",20);
		
		$sql = "select * from (select cs.login as uid,su.user,ca.name as area,cs.* 
                               from crm_customer as cs left join crm_area as ca on ca.id = cs.area_id,
                                    sys_user as su
                               where cs.login = su.id
		                      ) a 
		        where 1=1 ";
		
		if($pa["name"] != ""){$sql .= " and name like '%".$pa["name"]."%'";}
		if($pa["area"] != ""){$sql .= " and area_id in (".$pa["area"].")";}
		$sql .= " order by ".$sortField." ".$sortOrder;
		$sql .= " limit [start],[end]";

		return $this->sql($sql,array("start"=>$r*$p,"end"=>$r))->s();
	}
	
	
	// 未处理消息件数
	function BLogCount($p){
		$rs = $this->sql("select count(*) as c from crm_business_log where receive_flg=0 and msg_flg=1 and accept_flg=0 and receive=[cid]",$p)->s(1);
		return $rs['c'];
	}
	
	// 未处理取前3条
	function BLogList3($p){
		$rs = $this->sql("select id,content,created from crm_business_log where receive_flg=0 and msg_flg=1 and accept_flg=0 and receive=[cid] order by id desc limit 3",$p)->s();
		foreach ($rs as $item){
			$p1["id"] = $item['id'];
			$this->sql("update crm_business_log set accept_flg=1 where id=[id]",$p1)->u();
		}
		return $rs;
	}
	
	// 未处理信息列表
	function BLogListAll($p){
		return $this->sql("select content,created from crm_business_log where receive_flg=0 and msg_flg=1 and accept_flg=0 and receive=[cid] order by id desc ",$p)->s();
	}
}